package com.rzgeo.wcgallay;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.alibaba.fastjson.JSON;
import com.rzgeo.rzmaplib.util.IRmlSerializable;

import org.xwalk.core.JavascriptInterface;
import org.xwalk.core.XWalkPreferences;
import org.xwalk.core.XWalkSettings;
import org.xwalk.core.XWalkView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class WcmChartActv extends AppCompatActivity {

    private XWalkView mXwview;
    private XWalkSettings mMSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actv_chart);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //添加对javascript支持
        XWalkPreferences.setValue("enable-javascript", true);
//开启调式,支持谷歌浏览器调式
        XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
//置是否允许通过file url加载的Javascript可以访问其他的源,包括其他的文件和http,https等其他的源
        XWalkPreferences.setValue(XWalkPreferences.ALLOW_UNIVERSAL_ACCESS_FROM_FILE, true);
//JAVASCRIPT_CAN_OPEN_WINDOW
        XWalkPreferences.setValue(XWalkPreferences.JAVASCRIPT_CAN_OPEN_WINDOW, true);
// enable multiple windows.
        XWalkPreferences.setValue(XWalkPreferences.SUPPORT_MULTIPLE_WINDOWS, true);

        mXwview=findViewById(R.id.xw_web);
        //设置滑动样式。。。
        mXwview.setHorizontalScrollBarEnabled(false);
        mXwview.setVerticalScrollBarEnabled(false);
        mXwview.setScrollBarStyle(XWalkView.SCROLLBARS_OUTSIDE_INSET);
        mXwview.setScrollbarFadingEnabled(true);
//获取setting
        mMSettings = mXwview.getSettings();
//支持空间导航
        mMSettings.setSupportSpatialNavigation(true);
        mMSettings.setBuiltInZoomControls(true);
        mMSettings.setSupportZoom(true);
        mXwview.loadUrl("file:///android_asset/js/bar-animation-delay.html", null);
        mXwview.setDrawingCacheEnabled(false);//不使用缓存
        mXwview.getNavigationHistory().clear();//清除历史记录
        mXwview.clearCache(true);//清楚包括磁盘缓存

        StartInfo startInfo=(StartInfo)getIntent().getSerializableExtra(StartInfo.Key);

        mXwview.addJavascriptInterface(new LocalJs(startInfo),"WcmData");
    }

    public static class LocalJs{
        private final StartInfo _startInfo;

        public LocalJs(StartInfo startInfo){
            _startInfo=startInfo;
        }

        @JavascriptInterface
        public String getSetting(){
            Map<String,Object>map=new HashMap<>();
            map.put("title",_startInfo.title);
            map.put("xAxisData",_startInfo.xAxisData);
            map.put("legendDataNames",_startInfo.legendDataNames);
            map.put("datas",_startInfo.datas);
            return JSON.toJSONString(map);
        }
    }

    public static class StartInfo implements IRmlSerializable{
        public String title;
        public String[]xAxisData;
        public String[]legendDataNames;
        public double[][]datas;

        public StartInfo(String title, String[] xAxisData, String[] dataName,double[][]datas ){
            this.title = title;
            this.xAxisData = xAxisData;
            this.legendDataNames = dataName;
            this.datas=datas;
        }


        public static class StartInfoBuilder{
            private final double _defaultValue;
            private String title;
            private Comparator<? super String> _compare;
//            public Map<String,double[]> name2datas;

            public StartInfoBuilder(String title, Comparator<? super String> compare, double defaultValue) {
                this.title = title;
//                this.name2datas =new HashMap<>();
//                xAxis2Indexs=new HashMap<>();
                dataName2xAxis2Values=new HashMap<>();
                _compare=compare;
                _defaultValue=defaultValue;
            }


            public StartInfo toStartInfo(){
                Set<String>xAxiSet=new HashSet<>();
                for(String dataName:dataName2xAxis2Values.keySet()){
                    xAxiSet.addAll(dataName2xAxis2Values.get(dataName).keySet());
                }
                ArrayList<String>xAxis=new ArrayList<>(xAxiSet);
                Collections.sort(xAxis,_compare);

                String[] dataNames=dataName2xAxis2Values.keySet().toArray(new String[0]);
                double[][] values=new double[dataNames.length][xAxis.size()];

                for(int m=0;m<dataNames.length;m++){
                    String dataName=dataNames[m];

                    Map<String, Double> xAxis2Values = dataName2xAxis2Values.get(dataName);

                    for(int i=0;i<xAxis.size();i++){
                        String xA=xAxis.get(i);
                        Double value = xAxis2Values.get(xA);

                        if(value==null){
                            values[m][i]=_defaultValue;
                        }else{
                            values[m][i]=value;
                        }
                    }
                }

                return new StartInfo(
                        title,
                        xAxis.toArray(new String[0]),
                        dataNames,
                        values);
            }

            private Map<String,Map<String,Double>>dataName2xAxis2Values;
            public void addData(String dataName,String xAxis,double ydata){
                Map<String, Double> xAxis2Values = dataName2xAxis2Values.get(dataName);
                if(xAxis2Values==null){
                    xAxis2Values=new HashMap<>();
                    dataName2xAxis2Values.put(dataName,xAxis2Values);
                }
                xAxis2Values.put(xAxis,ydata);
            }
        }
    }
}
